package com.wyzz.hr.io.extend;

import java.util.*;

import com.wyzz.hr.util.Message;
import com.wyzz.hr.util.comparator.ObjectFieldComparator;

public class IOMessages {

	public IOMessages() {
		msgList = new ArrayList();
		msgFormat = "";
	}

	public IOMessages(String msgFormat) {
		msgList = new ArrayList();
		this.msgFormat = "";
		setMsgFormat(msgFormat);
	}

	public String getSingleMessage(IOMessageSingle ioMessageSingle) {
		String result = "";
		if (msgFormat.equalsIgnoreCase("excel")) {
			if (ioMessageSingle.getRowNum() != null)
				result = (new StringBuilder()).append(result).append("\u7B2C")
						.append(ioMessageSingle.getRowNum()).append("\u884C")
						.toString();
			if (ioMessageSingle.getColNum() != null) {
				if (result.length() > 0)
					result = (new StringBuilder()).append(result).append(
							"\uFF0C").toString();
				result = (new StringBuilder()).append(result).append("\u7B2C")
						.append(
								numToExcelColNum(ioMessageSingle.getColNum()
										.intValue())).append("\u5217")
						.toString();
			}
		}
		result = (new StringBuilder()).append(result).append(" ").toString();
		result = (new StringBuilder()).append(result).append(
				ioMessageSingle.getMessage()).toString();
		return result;
	}

	public void sortMessages(int sortIndex[]) throws Exception {
		if (sortIndex.length == 0)
			return;
		int sortNum = sortIndex.length <= 2 ? sortIndex.length : 2;
		String fieldNameArr[] = new String[sortNum];
		for (int i = 0; i < sortNum; i++)
			fieldNameArr[i] = ROWCOLNAME[sortIndex[i]];

		ObjectFieldComparator objComparator = new ObjectFieldComparator(
				IOMessageSingle.class, fieldNameArr, new int[0]);
		Collections.sort(msgList, objComparator);
	}

	public List getShowMessageByTopDetail() {
		List result = new ArrayList();
		List topList = new ArrayList();
		List detailList = new ArrayList();
		for (Iterator i$ = msgList.iterator(); i$.hasNext();) {
			IOMessageSingle ioMessageSingle = (IOMessageSingle) i$.next();
			if (ioMessageSingle.getRowNum() == null
					&& ioMessageSingle.getColNum() == null)
				topList.add(new Message(ioMessageSingle.getErrorType(),
						getSingleMessage(ioMessageSingle)));
			else
				detailList.add(new Message(ioMessageSingle.getErrorType(),
						getSingleMessage(ioMessageSingle)));
		}

		result.add(topList);
		result.add(detailList);
		return result;
	}

	public List getShowMessage() {
		List result = new ArrayList();
		IOMessageSingle ioMessageSingle;
		for (Iterator i$ = msgList.iterator(); i$.hasNext(); result
				.add(new Message(ioMessageSingle.getErrorType(),
						getSingleMessage(ioMessageSingle))))
			ioMessageSingle = (IOMessageSingle) i$.next();

		return result;
	}

	public boolean hasErrorMsg() {
		List result = new ArrayList();
		for (Iterator i$ = msgList.iterator(); i$.hasNext();) {
			IOMessageSingle ioMessageSingle = (IOMessageSingle) i$.next();
			if (ioMessageSingle.getErrorType().intValue() < 0)
				return true;
		}

		return false;
	}

	public boolean hasWarningMsg() {
		List result = new ArrayList();
		for (Iterator i$ = msgList.iterator(); i$.hasNext();) {
			IOMessageSingle ioMessageSingle = (IOMessageSingle) i$.next();
			if (ioMessageSingle.getErrorType().intValue() > 0)
				return true;
		}

		return false;
	}

	public String numToExcelColNum(int num) {
		if (num < 0 || num > 701)
			return "";
		int bgnChar = 65;
		String result = "";
		int first = num / 26 - 1;
		int second = num % 26;
		if (first >= 0)
			result = (new StringBuilder()).append(result).append(
					(char) (first + bgnChar)).toString();
		result = (new StringBuilder()).append(result).append(
				(char) (second + bgnChar)).toString();
		return result;
	}

	public void addMessageToFirst(IOMessageSingle ioMessageSingle) {
		msgList.add(0, ioMessageSingle);
	}

	public void addMessage(IOMessageSingle ioMessageSingle) {
		msgList.add(ioMessageSingle);
	}

	public void addMessage(List msgList) {
		IOMessageSingle ioMessageSingle;
		for (Iterator i$ = msgList.iterator(); i$.hasNext(); addMessage(ioMessageSingle))
			ioMessageSingle = (IOMessageSingle) i$.next();

	}

	public List getMsgList() {
		return msgList;
	}

	public String getMsgFormat() {
		return msgFormat;
	}

	public void setMsgFormat(String msgFormat) {
		if (msgFormat == null)
			this.msgFormat = "";
		else
			this.msgFormat = msgFormat;
	}

	private List msgList;
	private String msgFormat;
	public static final String FORMAT_EXCEL = "excel";
	public static final int ROW = 0;
	public static final int COL = 1;
	public static final String ROWCOLNAME[] = { "rowNum", "colNum" };

}
